package com.gcstorage.tvdev.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gcstorage.tvdev.view.LoadingDialog;
import com.trello.rxlifecycle3.components.support.RxFragment;




/**
 * Author: Sanpark
 * Description: Fragment 基类
 * Date: 2020/5/20 14:22
 */
public abstract class RxBaseFragment extends RxFragment {
    public AppCompatActivity mActivity;
    public View rootView;
    public LoadingDialog loadingDialog;
    public boolean isFirstLoad = true;

    protected int getLayoutId() {
        return -1;
    }

    protected View getRootView(ViewGroup container) {
        return null;
    }

    public abstract void initView();

    public abstract void initData();

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (AppCompatActivity) activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (getLayoutId() > 0) {
            rootView = inflater.inflate(getLayoutId(), null);
        } else {
            rootView = getRootView(container);
        }
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        if (useEventBus() && !EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this);
//        }
        initView();
        if (savedInstanceState != null) {
            initSave(savedInstanceState);
        }
        // initData();
    }

    //当前活动是否使用EventBus,默认不使用
    public boolean useEventBus() {
        return false;
    }

    //占位空响应
//    @Subscribe
//    public void placeholderEvent() {
//
//    }

    protected void initSave(Bundle save) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        if (EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().unregister(this);
//        }
    }

    public void showLoadingDialog(String message, boolean cancelable) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mActivity);
            loadingDialog.setCancelable(cancelable);
        }
        loadingDialog.show();
    }

    public void showLoadingDialog(boolean cancelable) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mActivity);
            loadingDialog.setCancelable(cancelable);
        }
        loadingDialog.show();
    }

    public void showLoadingDialog() {
        showLoadingDialog(true);
    }

    public void closeLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad) {
            isFirstLoad = false;
            initData();
        }
    }
}
